<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>器械出库</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>
<body>
<script type="text/javascript">
var editIndex = undefined;
//结束正在编辑的行
function endEditing() {
	if (editIndex == undefined) {
		return true
	}
	if ($('#qxInstrumentLibrary').datagrid('validateRow', editIndex)) {
		$('#qxInstrumentLibrary').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
//事件：单击单元格的事件
function onClickCell(index, field) {
	if (editIndex != index) {
		if (endEditing()) {
			$('#qxInstrumentLibrary').datagrid('selectRow', index).datagrid('beginEdit',
					index);
			var ed = $('#qxInstrumentLibrary').datagrid('getEditor', {
				index : index,
				field : field
			});
			if (ed) {
				($(ed.target).data('textbox') ? $(ed.target).textbox(
						'textbox') : $(ed.target)).focus();
			}
			editIndex = index;
		} else {
			setTimeout(function() {
				$('#qxInstrumentLibrary').datagrid('selectRow', editIndex);
			}, 0);
		}
	}
}

//结束编辑，同步数据
function onEndEdit(index, row) {
	var ed = $(this).datagrid('getEditor', {
		index : index,
		field : 'hisReturngoods.thNumber'
	});
	if(ed){
		row.cgUnit = $(ed.target).combobox('getText');
	}else{
		row.cgUnit = null;
	}
}

//添加行
function append() {
	if (endEditing()) {
		$('#qxInstrumentLibrary').datagrid('appendRow', {
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : ''
		});
		editIndex = $('#qxInstrumentLibrary').datagrid('getRows').length - 1;
		$('#qxInstrumentLibrary').datagrid('selectRow', editIndex);
		$('#qxInstrumentLibrary').datagrid('beginEdit',editIndex);
	}
}

//删除行
function removeit() {
	var ss=$('#qxInstrumentLibrary').datagrid('getRowIndex');
	$('#qxInstrumentLibrary').datagrid('deleteRow',ss);
	editIndex = undefined;
}

//应用
function accept() {
	if (endEditing()) {
		//$('#qxInstrumentLibrary').datagrid('acceptChanges');
		endEditing();
	}
}

//撤消
function reject() {
	$('#qxInstrumentLibrary').datagrid('rejectChanges');
	editIndex = undefined;
}

//获取数据
function getChanges() {
	var rows = $('#qxInstrumentLibrary').datagrid('getChanges');
	alert(rows.length + ' rows are changed!');
}


$(function(){
	$('#thpckqx').combogrid({
		onChange: function (newValue, oldValue) {
			var kcName=$('#thpckqx').combogrid('getText');
			$.getJSON("selectHisInventoryByName", {"kcName": kcName}, function(data){
				$('#thpckjcsl').numberbox('setValue',data.kcNumber);
				$('#thpcksl').numberspinner({max:data.kcNumber});
				$.getJSON("selectHisPurchaseByName", {"cpName": data.kcName}, function(data){
					$('#thpckph').textbox('setValue',data.cgBatchnumber);
				});
			});
		}
	});
	
	//出库数量
	$('#thpcksl').numberspinner({
		onChange : function (newValue , oldValue) {
			var thpckjcsl=$('#thpckjcsl').numberbox('getValue');
			if(thpckjcsl==''){
				$.messager.alert('温馨提示', '请先选择您需要出库的产品名称!','info');
				$('#thpcksl').numberspinner('setValue',0);
				$('#thpckjcsl').numberbox('setValue','');
			}else{
				//alert(typeof(thpckjcsl));
				var jcsl=$('#thpckjcsl').numberbox('getValue');
				//alert(typeof(parseInt(jcsl))+"+"+typeof(parseInt(oldValue))+"-"+typeof(parseInt(newValue)));
				var o=parseInt(jcsl)+parseInt(oldValue)-parseInt(newValue);
				//alert("oldValue="+oldValue);
				//alert("newValue="+newValue);
				$('#thpckjcsl').numberbox('setValue',o);
			}
		}
	});
	
	//数据加入出库临时表
	$('#add-qxckTable').linkbutton({
		onClick : function() {
			var thpckqxName=$('#thpckqx').combogrid('getText');
			$.getJSON("selectHisPurchaseByName", {"cpName": thpckqxName}, function(data){
				var unit=data.cgUnit;
				var id=data.cgNo;
				//alert(data.cgUnit);
				var thpckph=$('#thpckph').textbox('getValue');
				var thpckjcsl=$('#thpckjcsl').numberbox('getValue');
				var thpcksl=$('#thpcksl').numberspinner('getValue');
				var thpckbz=$('#thpckbz').textbox('getValue');
				
				var qxckState=$('#qxckState').combobox('getValue');
				var qxckDate=$('#qxckDate').datebox('getText');
				var qxckType=$('#qxckType').combobox('getText');
				var qxckObject=$('#qxckObject').combogrid('getText');
				var ckAdmin=$('#ckAdmin').combogrid('getText');
				var qxckck=$('#qxckck').combogrid('getText');
				var qxckjbr=$('#qxckjbr').combogrid('getText');
				
				var rows=$('#qxInstrumentLibrary').datagrid('getRows');
				var ii=0;
				for(var i=0;i<rows.length;i++){
					//alert(cgqx);
					var name=rows[i]['hisInstrumentlibraries.qxName'];
					//alert(name);
					if(name==thpckqxName){
						ii++;
					}
				}
				if(qxckDate=='' || qxckType=='' || qxckObject=='' || 
						ckAdmin=='' || qxckck=='' || qxckjbr=='' || thpckqxName=='' || parseInt(thpcksl)<1 ){
					$.messager.alert("温馨提示", "请把数据填充完整!",'info');
				}else if(ii>0){
					$.messager.alert("提示", "产品已存在临时出库中，请勿重复添加!");
					$('#AllWarehouseInfo').form("clear");
				}else{
					$('#qxInstrumentLibrary').datagrid('appendRow', {
						'ckName' : qxckck,
						'hisInstrumentlibraries.qxNo' : id,
						'hisInstrumentlibraries.qxName' : thpckqxName,
						'hisInstrumentlibraries.qxckBalance' : thpckjcsl,
						'cgUnit' : unit,
						'cgBatchnumber' : thpckph,
						'hisInstrumentlibraries.qxckNumber' : thpcksl,
						'hisInstrumentlibraries.qxckRemarks' : thpckbz
					});
					$('#AllWarehouseInfo').form("clear");
				}
			});
			
		}
	});
	
	//出库
	$('#submit-qxInstrumentLibrary').linkbutton({
		onClick : function() {
			//将datagrid中的数据行保存到隐式表单域中
			//1、获取新增的数据行
			var rows = $('#qxInstrumentLibrary').datagrid('getChanges');
			//为每一行生成对应的隐藏表单域
			var qxckState='已出库';
			var qxckType=$('#qxckType').combobox('getText');
			var qxckObject=$('#qxckObject').combogrid('getText');
			var ckAdmin=$('#ckAdmin').combogrid('getText');
			var qxckck=$('#qxckck').combogrid('getValue');
			var qxckjbr=$('#qxckjbr').combogrid('getValue');
			var today='${InstrumentLibraryId }'*1;
			var ff_ckInstrumentLibrary=$('#ff_ckInstrumentLibrary');
			for(var i = 0; i < rows.length; i ++){
				$("<input type='hidden' x='y'>").attr("name",
						"hisInstrumentlibraries[" + i + '].qxckNo').val(
								today+i).appendTo(ff_ckInstrumentLibrary);
				$("<input type='hidden' x='y'>").attr("name",
						"hisInstrumentlibraries[" + i + '].ckNo').val(
								qxckck).appendTo(ff_ckInstrumentLibrary);
				$("<input type='hidden' x='y'>").attr("name",
						"hisInstrumentlibraries[" + i + '].qxckLibraryobject').val(
								qxckObject).appendTo(ff_ckInstrumentLibrary);
				$("<input type='hidden' x='y'>").attr("name",
						"hisInstrumentlibraries[" + i + '].qxckState').val(
								qxckState).appendTo(ff_ckInstrumentLibrary);
				$("<input type='hidden' x='y'>").attr("name",
						"hisInstrumentlibraries[" + i + '].qxckType').val(
								qxckType).appendTo(ff_ckInstrumentLibrary);
				$("<input type='hidden' x='y'>").attr("name",
						"hisInstrumentlibraries[" + i + '].yg_manager').val(
								ckAdmin).appendTo(ff_ckInstrumentLibrary);
				$("<input type='hidden' x='y'>").attr("name",
						"hisInstrumentlibraries[" + i + '].ygNo').val(
								qxckjbr).appendTo(ff_ckInstrumentLibrary);
				generateHidden(rows[i], "qxNo", i);
				generateHidden(rows[i], "qxName", i);
				generateHidden(rows[i], "qxckBalance", i);
				generateHidden(rows[i], "qxckNumber", i);
				generateHidden(rows[i], "qxckRemarks", i);
			}
			//提交表单
			if(rows.length != 0){
				$('#ff_ckInstrumentLibrary').form('submit', {
					success: function(v){
						var json = eval("(" + v + ")");
						//alert(json);
						if(json=='操作成功'){
							$(":hidden[x='y']").remove();
							$('#qxInstrumentLibrary').datagrid('loadData', { total: 0, rows: [] });
							$.messager.alert('温馨提示', "出库"+json);
							$('#ff_ckInstrumentLibrary').form("clear");
							$("#win-InstrumentLibrary").window("close");
						}else{
							$.messager.alert('温馨提示', json,'info');
						}
					}
				});
			}
		}
	});
	
	function generateHidden(row, name, i) {
		var ff_ckInstrumentLibrary = $("#ff_ckInstrumentLibrary");
		$("<input type='hidden' x='y'>").attr("name",
				"hisInstrumentlibraries[" + i + '].'  + name).val(
				row['hisInstrumentlibraries.'+name]).appendTo(ff_ckInstrumentLibrary);
	}
});
</script>
<style type="text/css">
	*{
		font-size:12px;
	}
	#QXInstrumentLibraryinfo{
		width:100%;
		overflow: hidden;
	}
	li{
		float: left;
		list-style: none;
	}
</style>

	<!-- 器械出库 -->
	<!-- <div id="win-InstrumentLibrary" class="easyui-window" title="器械出库" data-options="collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-outgoing'"
		style="width: 806px; height: 500px;"> -->
		<form id="ff_ckInstrumentLibrary" action="QiXieInstrumentlibrary" method="post">
			<div id="QXInstrumentLibraryContent">
				<div id="QXInstrumentLibraryinfo" style="background-color: rgb(236,243,255)">
					<ul>
						<li style="margin:0px 60px 5px 0px;">出库单号&nbsp;<input value="${InstrumentLibraryId }" class="easyui-textbox" data-options="width: 150" readonly="readonly"></li>
						<li style="margin:0px 0px 5px 60px;">状态&nbsp;<select id="qxckState" class="easyui-combobox" data-options="width: 80,panelHeight:'auto'" readonly="readonly">
							<option value="未出库" selected="selected">未出库</option><option value="已出库">已出库</option></select></li>
						<li style="margin:0px 0px 5px 130px;">出库日期&nbsp;<input id="qxckDate" class="easyui-datebox" data-options="width: 100"></li>
						<li style="margin:0px 0px 5px 0px;">出库类型&nbsp;<select id="qxckType" class="easyui-combobox" data-options="width: 150,panelHeight:'auto'">
							<option value="领用出库" selected="selected">领用出库</option><option value="退货出库">退货出库</option></select></li>
						<li style="margin:0px 0px 5px 96px;">出库对象&nbsp;<input id="qxckObject" class="easyui-combogrid" data-options="width: 200,
							url:'selectAllHisDepartment',
							idField:'ksNo',
							panelHeight:'auto',
							textField:'ksName',
							columns:[[
					            {field:'ksName',title:'科室名称',width:196}
							]]"></li>
						<li style="margin:0px 0px 5px 22px;">仓管员&nbsp;<select id="ckAdmin" class="easyui-combogrid" data-options="width: 100,
							url:'findAllEmployeeWarehouse',
							idField:'ygNo',
							panelHeight:'auto',
							panelWidth:225,
							textField:'ygName',
							columns:[[
					            {field:'ygName',title:'姓名',width:50},
					            {field:'bmName',title:'部门',width:75},
					            {field:'ygPost',title:'职务',width:92}
							]]"></select></li>
						<li style="margin:0px 115px 10px 0px;">出库仓库&nbsp;<input id="qxckck" class="easyui-combogrid" data-options="width: 150,
							url:'selectAllHisWarehouse',
							idField:'ckNo',
							panelHeight:'auto',
							textField:'ckName',
							columns:[[
					            {field:'ckName',title:'仓库名称',width:'146px'}
							]]"></li>
						<li style="margin:0px 0px 10px 255px;">经办人&nbsp;<input id="qxckjbr" class="easyui-combogrid" data-options="width: 100,
							url:'findAllEmployee',
							idField:'ygNo',
							panelHeight:200,
							panelWidth:225,
							textField:'ygName',
							columns:[[
					            {field:'ygName',title:'姓名',width:50},
					            {field:'bmName',title:'部门',width:75},
					            {field:'ygPost',title:'职务',width:92}
							]]"></li>
					</ul>
				</div>
				<div style="border-top:1px solid #95B8e7;width:100%;height:320px;overflow: hidden;">
			        <div>
			        	<div id="AllWarehouseInfo" style="overflow: hidden;">
			        		<ul>
								<li style="margin:10px 35px 10px 0px;">器械&nbsp;<select id="thpckqx" class="easyui-combogrid" data-options="width: 200,
									url:'findAllHisInventory',
									idField:'kcNo',
									panelHeight:'200',
									textField:'kcName',
									columns:[[{field:'kcName',title:'仓库器械名称',width:'180px'}
									]]"></select></li>
								<li style="margin:10px 35px 10px 0px;">批号&nbsp;<input id="thpckph" class="easyui-textbox" data-options="width: 100" readonly="readonly"></li>
								<li style="margin:10px 25px 10px 0px;">结存数量&nbsp;<input id="thpckjcsl" class="easyui-numberbox" data-options="width: 100" readonly="readonly"></li>
								<li style="margin:10px 0px 10px 0px;">出库数量&nbsp;<input id="thpcksl" class="easyui-numberspinner" value='0' data-options="width: 80,min:0,editable:true"></li>
								<li style="margin:0px 0px 10px 0px;">备注&nbsp;<input id="thpckbz" class="easyui-textbox" data-options="width: 550"></li>
								<li style="margin:0px 0px 10px 75px;"><a id="add-qxckTable" class="easyui-linkbutton" style="width:80px;">加入</a></li>
							</ul>
			        	</div>
				        <div style="border:0px red solid;height:230px;">
			        		<table id="qxInstrumentLibrary" class="easyui-datagrid"
								data-options="
									fitColumns:true,
									fit:true,
									singleSelect: true,
									rownumbers: true,
									pagination: true,
									pageSize: 10,
									toolbar: '#qxInstrumentLibraryTB',
									pageList: [5,10,15,20],
									border:false
									">
								<thead>
									<tr>
										<th data-options="field:'ckName',width:'10%',editor:'textbox'">仓库名称</th>
										<th data-options="field:'hisInstrumentlibraries.qxNo',width:'13%',editor:'numberbox'">器械编号</th>
										<th data-options="field:'hisInstrumentlibraries.qxName',width:'15%',editor:'numberbox'">器械名称</th>
										<th data-options="field:'hisInstrumentlibraries.qxckBalance',width:'10%',editor:'numberbox'">结存数量</th>
										<th data-options="field:'cgUnit',width:'8%',editor:'numberbox'">单位</th>
										<th data-options="field:'cgBatchnumber',width:'15%',editor:'textbox'">批号</th>
										<th data-options="field:'hisInstrumentlibraries.qxckNumber',width:'10%',editor:'numberbox'">出库数量</th>
										<th data-options="field:'hisInstrumentlibraries.qxckRemarks',width:'20%',editor:'textbox'">备注</th>
									</tr>
								</thead>
							</table>
							<div id="qxInstrumentLibraryTB" style="height: auto;padding-left: 30px;">
								<a href="javascript:void(0)" class="easyui-linkbutton"
								data-options="iconCls:'icon-add',plain:true">添加一行</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-remove',plain:true"
									onclick="removeit()">移除一行</a> <a href="javascript:void(0)"
									class="easyui-linkbutton"
									data-options="iconCls:'icon-ok',plain:true">应用</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
							</div>
			        	</div>
			        </div>
			    </div>
			    <div style="margin:5px 5px 0px 0px;text-align: right;">
					<a id="submit-qxInstrumentLibrary" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">出库</a>
					<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:$('#win-InstrumentLibrary').window('close');" onclick="">取消</a>
				</div>
			</div>
		</form>
	<!-- </div> -->
</body>
</html>